package com.jinbo.footmessage.lookServers;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.httpbean.WorkerInformationBean;
import com.jinbo.footmessage.httpretrofit.HttpProtocolFactory;
import com.jinbo.footmessage.serverInf.ServerInfActivity;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by fangjinbo on 2016/3/25.
 */
public class ServerAdapter extends BaseAdapter implements SectionIndexer {
    private List<WorkerInformationBean> list = null;
    private Activity mActivity;

 /*   public long getNewFrienduid() {
        return newFrienduid;
    }

    public void setNewFrienduid(long newFrienduid) {
        this.newFrienduid = newFrienduid;
    }

    private long newFrienduid;*/

    public ServerAdapter(Activity mActivity, List<WorkerInformationBean> list) {
        this.mActivity = mActivity;

        this.list = list;
    }



    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List<WorkerInformationBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public void setList(List<WorkerInformationBean> list) {
        this.list = list;
    }

    public List<WorkerInformationBean> getList() {
        return list;
    }

    public int getCount() {
        return this.list.size();
    }

    public WorkerInformationBean getItem(int position) {
        return list.get(position - 1);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder holder;
        final WorkerInformationBean mContent = list.get(position);
        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(
                    R.layout.list_server_item, null);
            holder = new ViewHolder();
       /*   /*  holder.iv_head = (ImageCircleView) view
                    .findViewById(R.id.iv_head);*/
            holder.ll_detail = (LinearLayout) view
                    .findViewById(R.id.detail);
            holder.tv_name = (TextView) view
                    .findViewById(R.id.tv_name);
            holder.tv_title = (TextView) view
                    .findViewById(R.id.tv_title);
            holder.v_line=(View)view.findViewById(R.id.v_line);

            holder.head=(CircleImageView)view.findViewById(R.id.head);
            holder.order_number=(TextView)view.findViewById(R.id.order_number);
            holder.speciality=(TextView)view.findViewById(R.id.speciality);
            holder.star=(RatingBar)view.findViewById(R.id.star);
           
           
            //holder.red_point=(ImageView)view.findViewById(R.id.iv_new_friend);*/
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
         //LocalPreferencesHelper localPreferencesHelper = new LocalPreferencesHelper(mActivity, LocalPreferencesHelper.DB_NAME);

        holder.tv_name.setText(mContent.name);

        Glide.with(mActivity)
                .load(HttpProtocolFactory.HOST_URL_USER+mContent.getHead())
                        // .placeholder(R.mipmap.ic_launcher)
                        // .crossFade()
                .into(holder.head);
        holder.order_number.setText(mContent.getOrder_number()+"");
        holder.speciality.setText(mContent.getSpeciality());
        holder.star.setRating((float) mContent.getStar());
      //  holder.iv_head.setImageURL(Constant.NEW_ICON_HOST_URL + mContent.uid + "?big=0&v=" + localPreferencesHelper.getString(LocalPreferencesHelper.USER_HEAD_RAMDOM_CODE), R.drawable.icon_head_big);
        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(section)){
            holder.tv_title.setVisibility(View.VISIBLE);
            holder.tv_title.setText(mContent.sortLetters);
        }else{
            holder.tv_title.setVisibility(View.GONE);

        }
        if(position<list.size()-1) {
            int section2 = getSectionForPosition(position + 1);
            if (section!=section2)
                holder.v_line.setVisibility(View.GONE);
            else
                holder.v_line.setVisibility(View.VISIBLE);
        }
        if(position == list.size()-1)
            holder.v_line.setVisibility(View.GONE);

      /*  if(newFrienduid!=0){
            if(mContent.uid==newFrienduid){
                holder.red_point.setVisibility(View.VISIBLE);
            }else {
                holder.red_point.setVisibility(View.GONE);
            }
        }else{
            holder.red_point.setVisibility(View.GONE);
        }*/
        holder.ll_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* WorkerInformationBean bean=new WorkerInformationBean();
                bean.setId(mContent.id);
                bean.setName(mContent.name);
                bean.setOrder_number(mContent.order_number);
                bean.setHead(mContent.head);
                bean.setSpeciality(mContent.speciality);
                bean.setAge(mContent.age);
                bean.setSex(mContent.sex);
                bean.setStar(mContent.star);*/
               ServerInfActivity.launch(mActivity,mContent);
            }
        });
        return view;

    }


    private static class ViewHolder {
        TextView tv_title;
       // ImageCircleView iv_head;
        TextView tv_name;
        View v_line;
        LinearLayout ll_detail;
      //  ImageView red_point;

        CircleImageView head;
        TextView order_number;
        TextView speciality;
        RatingBar star;
        
        
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).sortLetters.charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).sortLetters;
            if(sortStr==null || sortStr.length() == 0){
                continue;
            }
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}